package com.yxhl.km.module.login.activity;

import android.Manifest;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yxhl.km.HomeActivity;
import com.yxhl.km.R;
import com.yxhl.km.base.BaseActivity;
import com.yxhl.km.module.login.entity.UserInfoBean;
import com.yxhl.km.module.login.mvp.LoginPrensenter;
import com.yxhl.km.module.utils.Constants;
import com.yxhl.km.module.utils.MD5Utils;
import com.yxhl.km.module.utils.SharedPreferencesUtils;
import com.yxhl.km.module.utils.ToastUtil;
import com.yxhl.km.module.utils.mission.OnPerMissionResult;
import com.yxhl.km.module.utils.mission.PerMissionUtils;
import com.yxhl.km.module.view.DiagUtil;
import com.yxhl.km.mvp.BaseView;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 张亚楠 on 2018/5/19.
 * 登录返回Bean
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.login_amount)
    EditText loginAmount;
    @BindView(R.id.login_pass)
    EditText loginPass;
    @BindView(R.id.ll_user)
    AutoLinearLayout llUser;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_password)
    TextView tvPassword;
    private String[] permission = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.INTERNET};
    private String[] namePermission = {"修改存储权限", "读取存储", "访问网络"};
    @Override
    protected void initListener() {
        tvLogin.setOnClickListener(this);
        tvPassword.setOnClickListener(this);
    }

    @Override
    protected void initialized(android.os.Bundle savedInstanceState) {
        PerMissionUtils.getInstance().getMission(mContext, permission, namePermission, new OnPerMissionResult() {
            @Override
            public void onPermissionGranted() {

            }

            @Override
            public void onNoPermission() {

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /**
             * 登录
             */
            case R.id.tv_login:
                if (TextUtils.isEmpty(loginAmount.getText().toString())) {
                    ToastUtil.makeToast("手机号码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(loginPass.getText().toString())) {
                    ToastUtil.makeToast("密码不能为空");
                    return;
                }
                loginOnclick();
                break;
            /**
             * 忘记密码
             */
            case R.id.tv_password:
                gotoActivity(ForgetPasswordActivity.class, false, null);
                break;
        }

    }

    private void loginOnclick() {
        DiagUtil.getInstance().createWaitDialog(LoginActivity.this);
        LoginPrensenter loginPrensenter = new LoginPrensenter(new BaseView<UserInfoBean>() {
            @Override
            public void onResult(UserInfoBean userInfoBean) {
                ToastUtil.makeToast("登录成功");
                DiagUtil.getInstance().closeWaitDialog();
                SharedPreferencesUtils.setObjectToShare(mContext, Constants.USERINFO, userInfoBean);
                SharedPreferencesUtils.setParam(mContext, Constants.PASSWORD, MD5Utils.encode(loginPass.getText().toString()));
                SharedPreferencesUtils.setParam(mContext, Constants.ACCOUNT, loginAmount.getText().toString());
                gotoActivity(HomeActivity.class, true, null);
            }
        });
        loginPrensenter.getLoginData(loginAmount.getText().toString(), MD5Utils.encode(loginPass.getText().toString()));
    }

    @Override
    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}